//
//  GHEditingViewD.m
//  Diary
//
//  Created by lanou3g on 15/10/31.
//  Copyright © 2015年 王艺拓. All rights reserved.
//

#import "GHEditingViewD.h"
#import "WYTTimeTool.h"

@interface GHEditingViewD ()

@property (nonatomic, assign) NSInteger authorLabelFontSize;

@end

@implementation GHEditingViewD

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addViews];
    }
    return self;
}

- (void)addViews
{
    self.authorLabelFontSize = 11;
    if ([WYTDevicesTool iPhone6_iPhone6s]) {
        self.authorLabelFontSize = 10;
    } else if ([WYTDevicesTool iPhone320_DEVICES]) {
        self.authorLabelFontSize = 9;
    }
    // 图片
    self.pic_img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth * 0.95, kScreenWidth * 0.95 / 3 * 4)];
    //self.pic_img.backgroundColor = [UIColor grayColor];
    self.pic_img.layer.contents = (id)[UIImage imageNamed:@"imgPlaceHolderEVD"].CGImage;
    self.pic_img.contentMode = UIViewContentModeScaleAspectFill;
    self.pic_img.layer.masksToBounds = YES;
    [self addSubview:self.pic_img];
    
    // touchImageButton
    self.touchImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.touchImageButton.frame = CGRectMake(0, 0, kScreenWidth * 0.95, kScreenHeight * 0.7);
    [self addSubview:self.touchImageButton];
    
    // 标题
    self.titleTextF = [[UITextField alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.pic_img.frame) * 1.01, kScreenWidth * 0.95, kScreenHeight*0.08)];
    self.titleTextF.placeholder = @"添加标题";
    self.titleTextF.font = [UIFont systemFontOfSize:20];
    self.titleTextF.textAlignment = NSTextAlignmentCenter;
    //    self.titleTextF.backgroundColor = [UIColor grayColor];
    [self addSubview:self.titleTextF];
    
    // 天气图标
    self.weatherImage = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - 125-kScreenHeight * 0.06, CGRectGetMaxY(self.titleTextF.frame) *0.99, kScreenHeight * 0.025, kScreenHeight * 0.025)];
    self.weatherImage.image = [UIImage imageNamed:@"heavyRain_gray"];
    //    self.weatherImage.backgroundColor = [UIColor redColor];
    [self addSubview:self.weatherImage];
    
    UIButton *weatherButton = [UIButton buttonWithType:UIButtonTypeCustom];
    weatherButton.frame = CGRectMake(0, CGRectGetMaxY(self.titleTextF.frame) *0.99, self.frame.size.width, kScreenHeight * 0.05);
    weatherButton.backgroundColor = [UIColor clearColor];
    [weatherButton addTarget:self action:@selector(weatherButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:weatherButton];
    
    // 作者和时间
    self.authorAndDateLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width - 140 - 5, CGRectGetMaxY(self.titleTextF.frame) *0.99, 140, self.frame.size.width * 0.028)];
    self.authorAndDateLabel.textAlignment = NSTextAlignmentRight;
    
    self.authorAndDateLabel.text = @"2015/10/31 12:09pm ";
    CGFloat newWidth = [self getWidthString:self.authorAndDateLabel.text Font:self.authorLabelFontSize];
    self.authorAndDateLabel.frame = CGRectMake(self.frame.size.width - newWidth - 5, CGRectGetMaxY(self.titleTextF.frame) *0.99, newWidth, self.frame.size.height * 0.028);
    self.authorAndDateLabel.font = [UIFont systemFontOfSize:self.authorLabelFontSize];
    self.authorAndDateLabel.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    //    self.authorAndDateLabel.backgroundColor = [UIColor brownColor];
    [self addSubview:self.authorAndDateLabel];
    
    self.weatherImage.frame = CGRectMake(self.frame.size.width - newWidth - 5 - self.frame.size.height * 0.028, CGRectGetMaxY(self.titleTextF.frame) *0.99, self.frame.size.height * 0.028, self.frame.size.height * 0.028);
    
    // 地址
    self.addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.authorAndDateLabel.frame)-3, kScreenWidth * 0.94, kScreenHeight * 0.028)];
    self.addressLabel.textAlignment = NSTextAlignmentRight;
    self.addressLabel.text = @"唐山市,路北区";
    self.addressLabel.font = [UIFont systemFontOfSize:self.authorLabelFontSize];
    self.addressLabel.textColor = [UIColor colorWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0f];
    [self addSubview:self.addressLabel];
    
    UISwipeGestureRecognizer *swipToHideKeyboard = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipToHideKeyboardChick:)];
    swipToHideKeyboard.direction = UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipToHideKeyboard];
    
    UISwipeGestureRecognizer *textViewSwipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(textViewHasBeenSwipeUp:)];
    textViewSwipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [self addGestureRecognizer:textViewSwipeUp];
    
}

- (void)swipToHideKeyboardChick:(UISwipeGestureRecognizer *)sender
{
    [self endEditing:YES];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"SWIPETOHIDEKEYBOARD" object:nil userInfo:nil];
}
- (void)textViewHasBeenSwipeUp:(UISwipeGestureRecognizer *)sender{
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"SWIPEUPTODESK" object:nil userInfo:nil];
}
- (void)weatherButtonDidClicked:(UIButton *)sender{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"TAPTOJUMPINTODIARYINFOVIEW" object:nil userInfo:nil];
}

- (CGFloat)getWidthString:(NSString *)string Font:(NSInteger)font
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName];
    CGRect labelFrame = [string boundingRectWithSize:CGSizeMake(1000000, kScreenHeight * 0.028) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return labelFrame.size.width;
}
@end
